﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Text;

namespace HZ.Supervise.Web.Secondphase.NewReport
{
	public partial class SortSourcePSL_Total : System.Web.UI.Page
	{
		HZ.Supervise.IBLL.IExecuteContext iEC = HZ.Supervise.BLLFactory.ExecuteContext();
		string source = "";

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				this.AreaControl1.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
				DataBind();
			}
		}

		/// <summary>
		/// 绑定数据方法
		/// YD
		/// 2011-3-30 19:18
		/// </summary>
		private void DataBind()
		{
			int t1 = 0;
			int t2 = 0;

			StringBuilder sb = new StringBuilder();

			string strGroupbyCondition = "";
			string orderby = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;//排序字段与方式

			if (ddlSortSource.SelectedValue != "全部")   //判断是否等于‘全部’  ‘是’不加条件‘否’加入采购类型的查询条件
			{
				sb.AppendFormat(" AND SortSource ='{0}'", ddlSortSource.SelectedValue);
			}
            if (!string.IsNullOrEmpty(AreaControl1.SelectedAreaID) && AreaControl1.SelectedAreaID != "510000")
			{
				if (AreaControl1.SelectedAreaID.Length == 4)
				{
					sb.AppendFormat(" AND areaid like '{0}%'", AreaControl1.SelectedAreaID);
					strGroupbyCondition = "substring(AreaID,0,5)";
				}
				else
				{
					sb.AppendFormat(" AND areaid ='{0}'", AreaControl1.SelectedAreaID);
					strGroupbyCondition = "AreaID";

				}
			}
			else
			{

				strGroupbyCondition = "substring(AreaID,0,3)";

			}

			System.Data.DataTable dt = iEC.ExecutePagePSL_SortSource(sb.ToString(), orderby, this.AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t1);
			System.Data.DataTable dt1 = iEC.ExecutePagePSL_SortSourceByArea(strGroupbyCondition, sb.ToString(), orderby, this.AspNetPager2.CurrentPageIndex, AspNetPager2.PageSize, out t2);

			AspNetPager1.RecordCount = t1;
			AspNetPager2.RecordCount = t2;

			gvwPSLInfo.PageSize = AspNetPager1.PageSize;
			gvByAreaname.PageSize = AspNetPager2.PageSize;

			HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");

			HZ.Supervise.Web.Function.AddColumn(dt, "WareHouseRate", "SortSource");

			HZ.Supervise.Web.Function.ChangeRate_PS(dt1, "DistributeRate");

			HZ.Supervise.Web.Function.AddColumn(dt1, "WareHouseRate", "SortSource");

			this.gvwPSLInfo.DataSource = dt;
			this.gvByAreaname.DataSource = dt1;
			this.gvByAreaname.DataBind();
			this.gvwPSLInfo.DataBind();
		}

		/// <summary>
		/// 查询数据事件
		/// 董辉
		/// 2011-3-30 19:17
		/// </summary>
		/// <param name="sender">事件发送源</param>
		/// <param name="e">事件参数</param>
		protected void btn_chaxun_Click(object sender, EventArgs e)
		{
			AspNetPager1.CurrentPageIndex = 1;
			AspNetPager2.CurrentPageIndex = 1;
			//DataBind();
		}

		/// <summary>
		/// 分页事件
		/// 董辉
		/// 2011-3-30 19:19
		/// </summary>
		/// <param name="sender">事件发送源</param>
		/// <param name="e">事件参数</param>
		protected void AspNetPager1_PageChanged(object sender, EventArgs e)
		{
			this.dv2.Attributes.Add("style", "display:block");
			Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>document.getElementById(\"btnSee\").value=\"隐藏县区数据\";</script>"); 
			DataBind();
		}
		protected void AspNetPager2_PageChanged(object sender, EventArgs e)
		{
			DataBind();
		}
		/// <summary>
		/// 导出数据事件
		/// 董辉
		/// 2011-3-30 19:19
		/// </summary>
		/// <param name="sender">事件发送源</param>
		/// <param name="e">事件参数</param>
		protected void btnExcel_Click(object sender, EventArgs e)
		{
			int t1 = 0;
			StringBuilder sb = new StringBuilder();

			string strGroupbyCondition = "";
			string orderby = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;//排序字段与方式

			if (ddlSortSource.SelectedValue != "全部")   //判断是否等于‘全部’  ‘是’不加条件‘否’加入采购类型的查询条件
			{
				sb.AppendFormat(" AND SortSource ='{0}'", ddlSortSource.SelectedValue);
			}
            if (!string.IsNullOrEmpty(AreaControl1.SelectedAreaID) && AreaControl1.SelectedAreaID != "510000")
			{
				if (AreaControl1.SelectedAreaID.Length == 4)
				{
					sb.AppendFormat(" AND areaid like '{0}%'", AreaControl1.SelectedAreaID);
					strGroupbyCondition = "substring(AreaID,0,5)";
				}
				else
				{
					sb.AppendFormat(" AND areaid ='{0}'", AreaControl1.SelectedAreaID);
					strGroupbyCondition = "AreaID";

				}
			}
			else
			{

				strGroupbyCondition = "substring(AreaID,0,3)";

			}

			System.Data.DataTable dt = iEC.ExecutePagePSL_SortSource(sb.ToString(), orderby, 1, 0, out t1);

			AspNetPager1.RecordCount = t1;

			gvwPSLInfo.PageSize = AspNetPager1.PageSize;
			gvByAreaname.PageSize = AspNetPager2.PageSize;

			HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");

			HZ.Supervise.Web.Function.AddColumn(dt, "WareHouseRate", "SortSource");

			ExportFile.Instance.ExportData(dt, gvwPSLInfo, this.Page);
		}

		/// <summary>
		/// 导出数据事件
		/// 曹鹏
		/// 2011年5月30日18:14:38
		/// </summary>
		/// <param name="sender">事件发送源</param>
		/// <param name="e">事件参数</param>
		protected void btnExcel_Click1(object sender, EventArgs e)
		{
			int t2 = 0;

			StringBuilder sb = new StringBuilder();

			string strGroupbyCondition = "";

			string orderby = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;//排序字段与方式

			if (ddlSortSource.SelectedValue != "全部")   //判断是否等于‘全部’  ‘是’不加条件‘否’加入采购类型的查询条件
			{
				sb.AppendFormat(" AND SortSource ='{0}'", ddlSortSource.SelectedValue);
			}
            if (!string.IsNullOrEmpty(AreaControl1.SelectedAreaID) && AreaControl1.SelectedAreaID != "510000")
			{
				if (AreaControl1.SelectedAreaID.Length == 4)
				{
					sb.AppendFormat(" AND areaid like '{0}%'", AreaControl1.SelectedAreaID);

					strGroupbyCondition = "substring(AreaID,0,5)";
				}
				else
				{
					sb.AppendFormat(" AND areaid ='{0}'", AreaControl1.SelectedAreaID);

					strGroupbyCondition = "AreaID";

				}
			}
			else
			{

				strGroupbyCondition = "substring(AreaID,0,3)";

			}

			System.Data.DataTable dt1 = iEC.ExecutePagePSL_SortSourceByArea(strGroupbyCondition, sb.ToString(), orderby, this.AspNetPager2.CurrentPageIndex, AspNetPager2.PageSize, out t2);

			AspNetPager2.RecordCount = t2;

			HZ.Supervise.Web.Function.ChangeRate_PS(dt1, "DistributeRate");

			HZ.Supervise.Web.Function.AddColumn(dt1, "WareHouseRate", "SortSource");

			ExportFile.Instance.ExportData(dt1, gvByAreaname, this.Page);
		}
	}
}
